package com.xuelang.pipeline.app.application.impl;

import cn.hutool.core.util.IdUtil;
import com.xuelang.pipeline.app.application.TraitService;
import com.xuelang.pipeline.app.application.converter.TraitDTOToTraitConverter;
import com.xuelang.pipeline.client.application.dto.TraitDTO;
import com.xuelang.pipeline.client.application.dto.TraitQuery;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.resource.dto.EnvironmentQuery;
import com.xuelang.pipeline.domain.application.Trait;
import com.xuelang.pipeline.domain.application.service.ComponentDomainService;
import com.xuelang.pipeline.domain.application.service.TraitDomainService;
import com.xuelang.pipeline.domain.resource.service.EnvironmentDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 运维特征服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class TraitServiceImpl implements TraitService {

    @Autowired
    TraitDomainService traintDomainService;
    @Autowired
    ComponentDomainService componentDomainService;
    @Autowired
    EnvironmentDomainService environmentDomainService;

    @Override
    public PageRecord<TraitDTO> pageList(PageParam<TraitQuery> dtoPageParam) {
        TraitQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new TraitQuery() : condition;
        PageRecord<Trait> pageRecord = traintDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream().map(TraitDTOToTraitConverter.INSTANCE::traintToTraitDTO).collect(Collectors.toList()));
    }

    @Override
    public List<TraitDTO> list(TraitQuery traintQuery) {
        return traintDomainService.list(traintQuery).stream().map(TraitDTOToTraitConverter.INSTANCE::traintToTraitDTO).collect(Collectors.toList());
    }

    @Override
    public Boolean add(TraitDTO traintDTO) {
        BizAssert.isTrue(componentDomainService.existsCode(traintDTO.getApplicationCode(), traintDTO.getComponentLabel()), "运维特征添加失败，对应组件编码不存在");
        BizAssert.isTrue(!environmentDomainService.list(EnvironmentQuery.builder().uuid(traintDTO.getUuid()).build()).isEmpty(), "运维特征添加失败，对应环境信息不存在");
        Trait traint = TraitDTOToTraitConverter.INSTANCE.traintDTOToTrait(traintDTO);

        traint.setUuid(IdUtil.fastSimpleUUID());
        return traintDomainService.save(traint);
    }

    @Override
    public Boolean update(TraitDTO traintDTO) {
        BizAssert.notNull(traintDTO.getUuid(), "更新运维特征失败，uuid不能为空");
        Trait traint = TraitDTOToTraitConverter.INSTANCE.traintDTOToTrait(traintDTO);
        return traintDomainService.update(traint);
    }

    @Override
    public TraitDTO query(String traintUuid) {
        BizAssert.notNull(traintUuid, "查询运维特征失败，uuid不能为空");
        Trait queryTrait =  traintDomainService.query(Trait.builder().uuid(traintUuid).build());
        return TraitDTOToTraitConverter.INSTANCE.traintToTraitDTO(queryTrait);
    }

    @Override
    public Boolean delete(String traintUuid) {
        BizAssert.notNull(traintUuid, "删除运维特征失败，uuid不能为空");
        return traintDomainService.delete(Trait.builder().uuid(traintUuid).build());
    }

}
